//
//  GHFormMakeViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/23.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHFormMakeViewController.h"
#import "STRadioButton.h"
@interface GHFormMakeViewController ()
@property(nonatomic, strong) STRadioButton                                  *radioButton;
@property(nonatomic, strong) NSArray<GHTableEasyModel*>                     *dataSouce;
@property(nonatomic, strong) GHTableEasyModel                     *userShenmaModel;
@property(nonatomic, strong) GHTableEasyModel                     *yuemodel;
@property(nonatomic, strong) GHTableEasyModel                     *moneymodel;//两中支付方式的金额
@end

@implementation GHFormMakeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"报单";
    [self configSubView];
    [self configDataSouce];
    [self sendBusnisDataRequest];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"提交"
                                                   titleColor:GH_YellowBackGroundColor
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:GH_BlackBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
- (void)configDataSouce{
 
    __weak typeof(self) weakSelf =  self;
    self.radioButton = [[STRadioButton alloc] initWithRadio:@[@"现金支付",@"神马值支付"] andWithFrame:CGRectMake(0, 0, 250, 65)];
    self.radioButton.makenumOfarrayChecked = 0;
    [self.radioButton setActionHandle:^(NSInteger index) {
        [weakSelf onSelectedRadidonButton];
    }];
    GHTableEasyModel * paymodel = [[GHTableEasyModel alloc] initWithTextString:@"支付方式" accessoryView:self.radioButton];
    //支付金额
    GHTableEasyModel * moneymodel = [[GHTableEasyModel alloc] initWithTextString:@"*现金支付金额" detailString:@""];
    GHTableEasyModel * yuemodel = [[GHTableEasyModel alloc] initWithTextString:@"商家余额" detailString:@""];
    GHTableEasyModel * pwdmodel = [[GHTableEasyModel alloc] initWithTextString:@"*支付密码" detailString:@""];
    self.yuemodel = yuemodel;
    self.moneymodel = moneymodel;
    self.userShenmaModel = [[GHTableEasyModel alloc] initWithTextString:@"买家当前神马值" detailString:@""];
    NSArray * sectionOne =@[
                            [[GHTableEasyModel alloc] initWithTextString:@"商家姓名" detailString:@""],
                            [[GHTableEasyModel alloc] initWithTextString:@"*买家账号" detailString:@""],
                            self.userShenmaModel,
                            paymodel,
                            [[GHTableEasyModel alloc] initWithTextString:@"*购物金额" detailString:@""],
                            moneymodel,
                            [[GHTableEasyModel alloc] initWithTextString:@"*商品货号" detailString:@""],
                            [[GHTableEasyModel alloc] initWithTextString:@"*商品分类" detailString:@""],
                            [[GHTableEasyModel alloc] initWithTextString:@"*商品名称" detailString:@""],
                            [[GHTableEasyModel alloc] initWithTextString:@"*商品数量" detailString:@""],
                            [[GHTableEasyModel alloc] initWithTextString:@"*买家备注" detailString:@""],
                            [[GHTableEasyModel alloc] initWithTextString:@"*卖家备注" detailString:@""],
                            yuemodel,
                            pwdmodel
                            ];
    
    self.dataSouce = sectionOne;
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 65;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * reuseIdentifier =  [NSString stringWithFormat:@"%ldindex",indexPath.row];
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    GHTableEasyModel * model = self.dataSouce[indexPath.row];
    cell.textLabel.text = model.textString;

    cell.detailTextLabel.text = model.detailString;
    if (indexPath.row == 13) {
        cell.detailTextLabel.text = model.detailString.length?@"******":@"";
    }
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 12) {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GHTableEasyModel * model = self.dataSouce[indexPath.row];
    if (indexPath.row == 1) {
        [self onSlecetdAccountCellWithModel:model indexPath:indexPath];
    }
    if (indexPath.row == 4) {
        [self onSlecetdMoneyCellWithModel:model indexPath:indexPath];
    }
    if (indexPath.row == 5) {
        [self onSlecetdPayMoneyCellWithModel:model indexPath:indexPath];
    }
    if (indexPath.row == 6) {
        [self onSlecetdGoodNumCellWithModel:model indexPath:indexPath];
    }
    if (indexPath.row == 7) {
        [self onSlecetdGoodTypeCellWithModel:model indexPath:indexPath];
    }
    if (indexPath.row == 8) {
        [self onSlecetdGoodNameCellWithModel:model indexPath:indexPath];
    }
    if (indexPath.row == 9) {
        [self onSlecetdGoodCountCellWithModel:model indexPath:indexPath];
    }
    if (indexPath.row == 10) {
        [self onSlecetdUserNoteCellWithModel:model indexPath:indexPath];
    }
    if (indexPath.row == 11) {
        [self onSlecetdBusnisiessNoteCellWithModel:model indexPath:indexPath];
    }
    if (indexPath.row == 13) {
        [self onSlecetdPwdCellWithModel:model indexPath:indexPath];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
//*账号
- (void)onSlecetdAccountCellWithModel:(GHTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
    placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
    STTableViewDetailController * vc =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                                                          [self sendUserShenmaReqeustWithAccount:text];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    vc.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [self.navigationController pushViewController:vc animated:YES];
}
//*购物金额
- (void)onSlecetdMoneyCellWithModel:(GHTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
    placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
    STTableViewDetailController * vc =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    vc.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [self.navigationController pushViewController:vc animated:YES];
}
//*支付金额，神马值和现金支付
- (void)onSlecetdPayMoneyCellWithModel:(GHTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
    placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
    STTableViewDetailController * vc =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    vc.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [self.navigationController pushViewController:vc animated:YES];
}
//商品货号
- (void)onSlecetdGoodNumCellWithModel:(GHTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
    placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
    STTableViewDetailController * vc =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    [self.navigationController pushViewController:vc animated:YES];
}
//商品分类
- (void)onSlecetdGoodTypeCellWithModel:(GHTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
    placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
    STTableViewDetailController * vc =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    [self.navigationController pushViewController:vc animated:YES];
}
//*商品名称
- (void)onSlecetdGoodNameCellWithModel:(GHTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
    placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
    STTableViewDetailController * vc =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    [self.navigationController pushViewController:vc animated:YES];
}
//*商品数量
- (void)onSlecetdGoodCountCellWithModel:(GHTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
    placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
    STTableViewDetailController * vc =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    vc.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [self.navigationController pushViewController:vc animated:YES];
}
//买家备注
- (void)onSlecetdUserNoteCellWithModel:(GHTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
    placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
    STTableViewDetailController * vc =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    [self.navigationController pushViewController:vc animated:YES];
}
//卖家备注
- (void)onSlecetdBusnisiessNoteCellWithModel:(GHTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
    placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
    STTableViewDetailController * vc =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    [self.navigationController pushViewController:vc animated:YES];
}
//支付密码
- (void)onSlecetdPwdCellWithModel:(GHTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
    placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
    STTableViewDetailController * vc =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    vc.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{
    //判断
    for (GHTableEasyModel * model in self.dataSouce) {
        if ([model.textString containsString:@"*"]) {
            if (!model.detailString.length) {
                NSString * placeHolder = [model.textString stringByReplacingOccurrencesOfString:@"*" withString:@""];
                placeHolder = [NSString stringWithFormat:@"请输入%@",placeHolder];
                [SVProgressHUD showErrorWithStatus:placeHolder];
                return;
            }
        }
    }
    [self sendMakeFormRequest];
}
- (void)onSelectedRadidonButton{
    
    self.moneymodel.textString = [NSString stringWithFormat:@"*%@金额",self.radioButton.checkedString];
    [self.tableView reloadData];
}
#pragma mark --NetWork Method
- (void)sendUserShenmaReqeustWithAccount:(NSString*)account{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    if (account.length) {
        [dic setObject:account forKey:@"mobile"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/get_now_wfpoints"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"now_wfpoints"]) {
                                                     id str = dataDic[@"now_wfpoints"];
                                                     if ([str isKindOfClass:[NSNull class]]) {
                                                         self.userShenmaModel.detailString = @"";
                                                         GHTableEasyModel * model = self.dataSouce[1];
                                                         model.detailString = @"";
                                                         [SVProgressHUD showErrorWithStatus:@"未找到该账号"];
                                                     }else{
                                                         self.userShenmaModel.detailString = str;
                                                     }
                                                     
                                                 }
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];

}
- (void)sendBusnisDataRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/declaration"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"user_info"]) {
                                                     NSDictionary * userDic = dataDic[@"user_info"];
                                                     GHTableEasyModel * model = self.dataSouce.firstObject;
                                                     if ([userDic.allKeys containsObject:@"username"]) {
                                                         model.detailString = userDic[@"username"];
                                                     }
                                                     if ([userDic.allKeys containsObject:@"u_money"]) {
                                                         self.yuemodel.detailString = userDic[@"u_money"];
                                                     }
                                                     
                                                 }
                                                 if ([dataDic.allKeys containsObject:@"percent"]) {
                                                     //NSString * percent = dataDic[@"percent"];
                                                     
                                                 }
                                                  [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendMakeFormRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    GHTableEasyModel * shopping_money = self.dataSouce[4];
    if (shopping_money.detailString.length) {
        [dic setObject:shopping_money.detailString forKey:@"shopping_money"];
    }
    
    GHTableEasyModel * goods_number = self.dataSouce[6];
    if (goods_number.detailString.length) {
        [dic setObject:goods_number.detailString forKey:@"goods_number"];
    }
    
    GHTableEasyModel * goods_name = self.dataSouce[8];
    if (goods_name.detailString.length) {
        [dic setObject:goods_name.detailString forKey:@"goods_name"];
    }
    
    GHTableEasyModel * goods_cate = self.dataSouce[7];
    if (goods_cate.detailString.length) {
        [dic setObject:goods_cate.detailString forKey:@"goods_cate"];
    }
    
    GHTableEasyModel * goods_count = self.dataSouce[9];
    if (goods_count.detailString.length) {
        [dic setObject:goods_count.detailString forKey:@"goods_count"];
    }
    
    GHTableEasyModel * buy_note = self.dataSouce[10];
    if (buy_note.detailString.length) {
        [dic setObject:buy_note.detailString forKey:@"buy_note"];
    }
    
    GHTableEasyModel * business_note = self.dataSouce[11];
    if (business_note.detailString.length) {
        [dic setObject:business_note.detailString forKey:@"business_note"];
    }
    
    NSInteger num = self.radioButton.checkedNum.integerValue + 1;
    
    [dic setObject:@(num).description forKey:@"pay"];
    
    GHTableEasyModel * buy_username = self.dataSouce[1];
    if (buy_username.detailString.length) {
        [dic setObject:buy_username.detailString forKey:@"buy_username"];
    }
    
    GHTableEasyModel * pay_password = self.dataSouce[13];
    if (pay_password.detailString.length) {
         [dic setObject:pay_password.detailString forKey:@"pay_password"];
    }
    
    if (self.radioButton.checkedNum.integerValue == 0) {
        if (self.moneymodel.detailString.length) {
            [dic setObject:self.moneymodel.detailString forKey:@"cash_money"];
        }
    }else{
        if (self.moneymodel.detailString.length) {
            [dic setObject:self.moneymodel.detailString forKey:@"smz_money"];
        }
    }

    
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/add_report"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"报单成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
